﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Windows;
using Silvermoon.Controls;
using Silvermoon.Controls.Forms;
using Passwords.Controllers;
using Passwords.Biz;
using Passwords.DAL;
using Passwords.Pages;
using System.Threading;
using Silvermoon.Core;

namespace Passwords
{
    public static class PasswordCommands
    {
        public static readonly Command Password = new Command(RoutingStrategy.Bubble);
        public static readonly Command NewPassword = new Command(RoutingStrategy.Direct);
        public static readonly Command FilterPasswords = new Command(RoutingStrategy.Bubble);
        public static readonly Command DeletePassword = new Command(RoutingStrategy.Bubble);
        public static readonly Command ChangeFave = new Command(RoutingStrategy.Bubble);
        public static readonly Command PasswordOK = new Command(RoutingStrategy.Direct);
        public static readonly Command SavePassword = new Command(RoutingStrategy.Bubble);
        public static readonly Command EditPassword = new Command(RoutingStrategy.Bubble);
        public static readonly Command EditCategories = new Command(RoutingStrategy.Direct);
        public static readonly Command Debug = new Command(RoutingStrategy.Bubble);
        public static readonly Command CategoryChosen = new Command(RoutingStrategy.Bubble);
        public static readonly Command ChangePassword = new Command(RoutingStrategy.Direct);


        static PasswordCommands()
        {
            DeletePassword.Executed += new EventHandler<CommandEventArgs>(OnDeletePassword);
            SavePassword.Executed += new EventHandler<CommandEventArgs>(OnSavePassword);
            ChangeFave.Executed += new EventHandler<CommandEventArgs>(OnChangeFave);
            ChangePassword.Executed += new EventHandler<CommandEventArgs>(OnChangePassword);
        }

        static void OnChangePassword(object sender, CommandEventArgs e)
        {
        }

        static void OnChangeFave(object sender, CommandEventArgs e)
        {
            Password password = AppController.CurrentPassword;
            if (password != null)
            {
                password.IsFavorite ^= true;
                try
                {
                    password.Save();
                }
                catch (Exception ex)
                {
                    MessageDialog.Show("Error", "Could not add or remove password " + password.Name + " from favorites.\n" + ex.Message, null, null);
                }
            }
        }

        static void OnSavePassword(object sender, CommandEventArgs e)
        {
            Password password = AppController.CurrentPassword;
            try
            {
                if (password != null)
                {
                    password.Save();
                }
            }
            catch (Exception ex)
            {
                MessageDialog.Show("Could not save password", ex.Message);
            }
        }


        static void OnDeletePassword(object sender, CommandEventArgs e)
        {
            Password password = AppController.CurrentPassword;
            if (password != null)
            {
                MessageDialog.Show("Delete password?", password.Name + " will be deleted from your phone.", "Delete", "Cancel",
                    () => password.Delete(), null);
            }
        }
    }
}

